package com.mypage.view.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudcc.cloudframe.net.ErrorInfo;
import com.cloudcc.cloudframe.net.ResultCallBack;
import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.adapter.TmVerticalTimelineAdapter;
import com.cloudcc.mobile.dao.impl.ApprovalEngineImpl;
import com.cloudcc.mobile.entity.approval.TaskApprovalHistoryModel;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.view.activity.BeauInfoActivity;
import com.cloudcc.mobile.view.activity.Myinformation;
import com.cloudcc.mobile.view.activity.UserInfoActivity;
import com.litesuits.android.async.Log;
import com.mypage.utils.SaveTemporaryData;
import com.mypage.view.MyListView;
import com.mypage.view.MyScrollView;
import com.mypage.view.MyScrollView.OnGetBottomListener;

import java.util.ArrayList;
import java.util.List;

public class DigestFragment extends Fragment implements OnGetBottomListener,
		ResultCallBack<List<TaskApprovalHistoryModel>> {
	private View mView;
	private TmVerticalTimelineAdapter vadapter;
	private MyListView vertical;
	// private PtrClassicFrameLayout mptrframe;
	private ArrayList<TaskApprovalHistoryModel> modellist2;
	private ArrayList<TaskApprovalHistoryModel.Instancehis> modellist3;
	private String workItemId;
	private String objecttype;
	private String relatedId;
	private String ownername;
	private String ownerid;
	//所有人的id
	private String actorId;
	private String ACTION = "com.cloudcc.mobile.syncWeb";
	private ApprovalEngineImpl engine = new ApprovalEngineImpl();

//	private TextView mTextChance;// 业务机会
//	private TextView mTextName;// 发起人名字
//	private TextView mTextTime;// 发起时间
//	private TextView mTextDigest;// 摘要
	//审批对象名称，类型，所有人，发起时间，摘要
	private TextView shenpi_name,shenpi_leixing,owner,mTextTime,mTextDigest;
	//备注
	private EditText shenpi_beizhu;
	/**
	 * 自定义ScrollView嵌套ListView
	 */
	MyScrollView mMainScroll;

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		mView = View.inflate(getActivity(), R.layout.digest_fragment, null);

		initframeViews(mView);
		initadapter();
		initData();
		initReceiver();
		return mView;
	}

	BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(ACTION)) {
				getActivity().finish();
			}
		}
	};
	private LinearLayout linear;
	private LinearLayout.LayoutParams lp;

	public void initReceiver() {
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(ACTION);
		registerReceiver(receiver, intentFilter);
	}

	private void registerReceiver(BroadcastReceiver receiver2,
			IntentFilter intentFilter) {
		// TODO Auto-generated method stub

	}

	private void initData() {
		// TODO Auto-generated method stub

	}
String length;
	private void initframeViews(View view) {
		// TODO Auto-generated method stub
		/*
		 * 测量高度，系定义ScrollView嵌套listView
		 */
		mMainScroll = (MyScrollView) view.findViewById(R.id.main_scroll);
		linear = (LinearLayout) view.findViewById(R.id.linearLayout);
//		final DisplayMetrics displayMetrics = new DisplayMetrics();
//		getActivity().getWindowManager().getDefaultDisplay()
//				.getMetrics(displayMetrics);
//		final int height = displayMetrics.heightPixels; // 获取手机高度
//		lp = new LinearLayout.LayoutParams(
//				LinearLayout.LayoutParams.MATCH_PARENT, (height / 4) * 3);
//
//		linear.setLayoutParams(lp);
//		/*
//		 * 测量TextView控件的高度
//		 */
//		TextView textview = (TextView) view.findViewById(R.id.textview);
//		lp = new LinearLayout.LayoutParams(
//				LinearLayout.LayoutParams.MATCH_PARENT, height / 14);
//
//		textview.setLayoutParams(lp);
		mMainScroll.setBottomListener(this);
//		shenpi_leixing,owner,mTextTime,mTextDigest;
        shenpi_name = (TextView) view.findViewById(R.id.approval_xinxi_name);// 姓名
		owner = (TextView) view.findViewById(R.id.approval_xinxi_owner_value);// 所有人
		mTextTime = (TextView) view.findViewById(R.id.approval_xinxi_shijian_value);// 发起时间
		mTextDigest = (TextView) view.findViewById(R.id.textDigest);// 摘要
		shenpi_leixing = (TextView) view.findViewById(R.id.approval_leixing_value);// 类型


		shenpi_leixing.setText(getArguments().getString("objecttype"));
		owner.setText(getArguments().getString("ownername"));
		mTextTime.setText(getArguments().getString("sendTime"));
		shenpi_name.setText(getArguments().getString("name"));
		if (getArguments().getString("gigest") == null
				|| "".equals(getArguments().getString("gigest"))) {
//			mTextDigest
//					.setText("亲，目前没有配置审批页面布局，无法显示摘要，请您到批准过程那里去修改审批页面布局即可显示。");
            mTextDigest.setText("无摘要");
		} else {
			mTextDigest.setText(getArguments().getString("gigest"));
		}

		// objecttype = getArguments().getString("objecttype");
		relatedId = getArguments().getString("relatedId");
		actorId =  getArguments().getString("actorid");
		ownername = getArguments().getString("ownername");
		ownerid = getArguments().getString("ownerid");
		// mptrframe = (PtrClassicFrameLayout) mView
		// .findViewById(R.id.task_statuslist_loadmore);
		vertical = (MyListView) view.findViewById(R.id.vertical_timeline);
		vertical.setFocusable(false);
		refreshData();
		//审批时间名称点击事件
        shenpi_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), BeauInfoActivity.class);
                intent.putExtra("web",relatedId);
                startActivity(intent);
            }
        });
		//所有人点击事件
		owner.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				Intent intent = new Intent(getActivity(),
						Myinformation.class);

				intent.putExtra(
						UserInfoActivity.KEY_USERID, actorId);
				if(vadapter.getId()==null){
					return;
				}
				if (RunTimeManager.getInstance().getUserId().equals(vadapter.getId())) {
					intent.putExtra("isme", "cowme");
				}else{
					intent.putExtra("isme", "lyj");
				}

				intent.putExtra("userId", ownerid);
				intent.putExtra("name",ownername);
				SaveTemporaryData.mSmart = "";

								SaveTemporaryData.detailDyamic = "";
				SaveTemporaryData.General = false;
				SaveTemporaryData.GeneralOne = true;




				startActivity(intent);







			}
		});
	}
	private void initadapter() {
		// TODO Auto-generated method stub
		vadapter = new TmVerticalTimelineAdapter(getActivity());
		vertical.setAdapter(vadapter);
		for (int i = 0; i < vadapter.getheight(); i++) {
			View view1 = vadapter.getView(i, null, vertical);
			view1.measure(0, 0);
			length = length + view1.getMeasuredHeight();
			Log.d("sdfjcs",length+"哈哈哈");
		}
		String  i = length;
	}

	public void refreshData() {
		modellist2 = new ArrayList<TaskApprovalHistoryModel>();
		modellist3 = new ArrayList<>();
		engine.getApprovalHistory(relatedId, this);
	}

	@Override
	public void onSuccess(List<TaskApprovalHistoryModel> t) {
		// TODO Auto-generated method stub
		for (TaskApprovalHistoryModel taskApprovalHistoryModel : t) {
			modellist2.add(taskApprovalHistoryModel);
		}
//		if(t.size()>0){
//			LinearLayout.LayoutParams lp;
//			lp = new LinearLayout.LayoutParams(
//					LinearLayout.LayoutParams.MATCH_PARENT, t.size()*300);
//			linear.setLayoutParams(lp);
//		}
		for (int i=0;i<modellist2.size();i++){
			for (int j=0;j<modellist2.get(i).getInstancehis().size();j++){
				modellist3.add(modellist2.get(i).getInstancehis().get(j));
			}
		}
		if(modellist3.size()>0){
			LinearLayout.LayoutParams lp;
			lp = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.MATCH_PARENT, modellist3.size()*300);
			linear.setLayoutParams(lp);
		}
		boolean isvisible = false;
		vadapter.changeData(modellist3);
		if (modellist2.size() > 0) {
			workItemId = modellist2.get(0).getInstancehis().get(0).workitemId;
			String workItemId1 = modellist2.get(0).getInstancehis().get(0).workItemId;
			if (StringUtils.isEmpty(workItemId)) {
				if (StringUtils.isEmpty(workItemId1)) {
					isvisible = false;
				} else {
					workItemId = workItemId1;
					isvisible = true;
				}

			} else {
				isvisible = true;
			}
		} else {
			isvisible = false;
		}


		// mptrframe.refreshComplete();
	}

	@Override
	public void onFailure(ErrorInfo error) {
		// TODO Auto-generated method stub
		Toast.makeText(getActivity(), "加载失败，请重试", Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onBottom() {
		// TODO Auto-generated method stub
		vertical.setBottomFlag(true);
	}
}
